import { AxiosPromise } from 'axios'
import request from '@/hooks/utils/http'
import { UserQuery, UserVO } from '@/service/UserService'

export class RoleService {
    static listRole = (query: RoleQuery): AxiosPromise<RoleVO[]> => {
        return request({
            url: '/system/role/list',
            method: 'get',
            params: query
        });
    };

    /**
     * 通过roleIds查询角色
     * @param roleIds
     */
    static optionSelect = (roleIds: (number | string)[]): AxiosPromise<RoleVO[]> => {
        return request({
            url: '/system/role/optionselect?roleIds=' + roleIds,
            method: 'get'
        });
    };

    /**
     * 查询角色详细
     */
    static getRole = (roleId: string | number): AxiosPromise<RoleVO> => {
        return request({
            url: '/system/role/' + roleId,
            method: 'get'
        });
    };

    /**
     * 新增角色
     */
    static addRole = (data: any) => {
        return request({
            url: '/system/role',
            method: 'post',
            data: data
        });
    };

    /**
     * 修改角色
     * @param data
     */
    static updateRole = (data: any) => {
        return request({
            url: '/system/role',
            method: 'put',
            data: data
        });
    };

    /**
     * 角色数据权限
     */
    static dataScope = (data: any) => {
        return request({
            url: '/system/role/dataScope',
            method: 'put',
            data: data
        });
    };

    /**
     * 角色状态修改
     */
    static changeRoleStatus = (roleId: string | number, status: string) => {
        const data = {
            roleId,
            status
        };
        return request({
            url: '/system/role/changeStatus',
            method: 'put',
            data: data
        });
    };

    /**
     * 删除角色
     */
    static delRole = (roleId: Array<string | number> | string | number) => {
        return request({
            url: '/system/role/' + roleId,
            method: 'delete'
        });
    };

    /**
     * 查询角色已授权用户列表
     */
    static allocatedUserList = (query: UserQuery): AxiosPromise<UserVO[]> => {
        return request({
            url: '/system/role/authUser/allocatedList',
            method: 'get',
            params: query
        });
    };

    /**
     * 查询角色未授权用户列表
     */
    static unallocatedUserList = (query: UserQuery): AxiosPromise<UserVO[]> => {
        return request({
            url: '/system/role/authUser/unallocatedList',
            method: 'get',
            params: query
        });
    };

    /**
     * 取消用户授权角色
     */
    static authUserCancel = (data: any) => {
        return request({
            url: '/system/role/authUser/cancel',
            method: 'put',
            data: data
        });
    };

    /**
     * 批量取消用户授权角色
     */
    static authUserCancelAll = (data: any) => {
        return request({
            url: '/system/role/authUser/cancelAll',
            method: 'put',
            params: data
        });
    };

    /**
     * 授权用户选择
     */
    static authUserSelectAll = (data: any) => {
        return request({
            url: '/system/role/authUser/selectAll',
            method: 'put',
            params: data
        });
    };
// 根据角色ID查询部门树结构
    static deptTreeSelect = (roleId: string | number): AxiosPromise<RoleDeptTree> => {
        return request({
            url: '/system/role/deptTree/' + roleId,
            method: 'get'
        });
    };
}

/**
 * 菜单树形结构类型
 */
export interface DeptTreeOption {
    id: string;
    label: string;
    parentId: string;
    weight: number;
    children?: DeptTreeOption[];
}

export interface RoleDeptTree {
    checkedKeys: string[];
    depts: DeptTreeOption[];
}

export interface RoleVO extends BaseEntity {
    roleId: string | number;
    roleName: string;
    roleKey: string;
    roleSort: number;
    dataScope: string;
    menuCheckStrictly: boolean;
    deptCheckStrictly: boolean;
    status: string;
    delFlag: string;
    remark?: any;
    flag: boolean;
    menuIds?: Array<string | number>;
    deptIds?: Array<string | number>;
    admin: boolean;
}

export interface RoleQuery extends PageQuery {
    roleName: string;
    roleKey: string;
    status: string;
}

export interface RoleForm {
    roleName: string;
    roleKey: string;
    roleSort: number;
    status: string;
    menuCheckStrictly: boolean;
    deptCheckStrictly: boolean;
    remark: string;
    dataScope?: string;
    roleId: string | undefined;
    menuIds: Array<string | number>;
    deptIds: Array<string | number>;
}
